import request from "@/utils/request";

// #region 私聊音视频通话 API

/**
 * 呼叫视频/音频通话
 * @param {number} uid 对方用户ID
 * @param {string} mode 'video' 或 'voice'
 * @param {object} offer SDP offer对象
 */
export function rtcCallApi(uid, mode, offer) {
  return request({
    url: `/webrtc/private/call`,
    method: "post",
    params: { uid, mode },
    headers: { "Content-Type": "application/json; charset=utf-8" },
    data: JSON.stringify(offer),
  });
}

/**
 * 接受通话
 * @param {number} uid 对方用户ID
 * @param {object} answer SDP answer对象
 */
export function rtcAcceptApi(uid, answer) {
  return request({
    url: `/webrtc/private/accept`,
    method: "post",
    params: { uid },
    headers: { "Content-Type": "application/json; charset=utf-8" },
    data: JSON.stringify(answer),
  });
}

/**
 * 拒绝通话
 * @param {number} uid 对方用户ID
 */
export function rtcRejectApi(uid) {
  return request({
    url: `/webrtc/private/reject`,
    method: "post",
    params: { uid },
  });
}

/**
 * 取消呼叫
 * @param {number} uid 对方用户ID
 */
export function rtcCancelApi(uid) {
  return request({
    url: `/webrtc/private/cancel`,
    method: "post",
    params: { uid },
  });
}

/**
 * 呼叫失败
 * @param {number} uid 对方用户ID
 * @param {string} reason 失败原因
 */
export function rtcFailedApi(uid, reason) {
  return request({
    url: `/webrtc/private/failed`,
    method: "post",
    params: { uid, reason },
  });
}

/**
 * 挂断通话
 * @param {number} uid 对方用户ID
 */
export function rtcHandupApi(uid) {
  return request({
    url: `/webrtc/private/handup`,
    method: "post",
    params: { uid },
  });
}

/**
 * 同步 ICE Candidate
 * @param {number} uid 对方用户ID
 * @param {object} candidate ICE candidate对象
 */
export function rtcSendCandidateApi(uid, candidate) {
  return request({
    url: `/webrtc/private/candidate`,
    method: "post",
    params: { uid },
    headers: { "Content-Type": "application/json; charset=utf-8" },
    data: JSON.stringify(candidate),
  });
}

/**
 * WebRTC 心跳
 * @param {number} uid 对方用户ID
 */
export function rtcHeartbeatApi(uid) {
  return request({
    url: `/webrtc/private/heartbeat`,
    method: "post",
    params: { uid },
  });
}

// #region 群聊音视频通话 API (预设接口，后端待实现)

/**
 * 创建群视频通话
 * @param {number} groupId 群ID
 * @param {Array<object>} userInfos 成员信息
 */
export function rtcGroupSetupApi(groupId, userInfos) {
  return request({
    url: "/webrtc/group/setup",
    method: "post",
    data: { groupId, userInfos },
  });
}

/**
 * 接受群视频通话
 * @param {number} groupId 群ID
 */
export function rtcGroupAcceptApi(groupId) {
  return request({
    url: "/webrtc/group/accept",
    method: "post",
    params: { groupId },
  });
}

/**
 * 拒绝群视频通话
 * @param {number} groupId 群ID
 */
export function rtcGroupRejectApi(groupId) {
  return request({
    url: "/webrtc/group/reject",
    method: "post",
    params: { groupId },
  });
}

/**
 * 群视频通话失败
 * @param {number} groupId 群ID
 * @param {string} reason 失败原因
 */
export function rtcGroupFailedApi(groupId, reason) {
  return request({
    url: "/webrtc/group/failed",
    method: "post",
    data: { groupId, reason },
  });
}

/**
 * 加入群视频通话
 * @param {number} groupId 群ID
 */
export function rtcGroupJoinApi(groupId) {
  return request({
    url: "/webrtc/group/join",
    method: "post",
    params: { groupId },
  });
}

/**
 * 邀请加入群视频通话
 * @param {number} groupId 群ID
 * @param {Array<object>} userInfos 成员信息
 */
export function rtcGroupInviteApi(groupId, userInfos) {
  return request({
    url: "/webrtc/group/invite",
    method: "post",
    data: { groupId, userInfos },
  });
}

/**
 * 发送群视频offer
 * @param {number} groupId 群ID
 * @param {number} userId 用户ID
 * @param {object} offer SDP offer对象
 */
export function rtcGroupOfferApi(groupId, userId, offer) {
  return request({
    url: "/webrtc/group/offer",
    method: "post",
    data: { groupId, userId, offer },
  });
}

/**
 * 发送群视频answer
 * @param {number} groupId 群ID
 * @param {number} userId 用户ID
 * @param {object} answer SDP answer对象
 */
export function rtcGroupAnswerApi(groupId, userId, answer) {
  return request({
    url: "/webrtc/group/answer",
    method: "post",
    data: { groupId, userId, answer },
  });
}

/**
 * 退出群视频通话
 * @param {number} groupId 群ID
 */
export function rtcGroupQuitApi(groupId) {
  return request({
    url: "/webrtc/group/quit",
    method: "post",
    params: { groupId },
  });
}

/**
 * 取消群视频通话
 * @param {number} groupId 群ID
 */
export function rtcGroupCancelApi(groupId) {
  return request({
    url: "/webrtc/group/cancel",
    method: "post",
    params: { groupId },
  });
}

/**
 * 发送群视频candidate
 * @param {number} groupId 群ID
 * @param {number} userId 用户ID
 * @param {object} candidate ICE candidate对象
 */
export function rtcGroupCandidateApi(groupId, userId, candidate) {
  return request({
    url: "/webrtc/group/candidate",
    method: "post",
    data: { groupId, userId, candidate },
  });
}

/**
 * 群视频设备变更
 * @param {number} groupId 群ID
 * @param {boolean} isCamera 是否开启摄像头
 * @param {boolean} isMicroPhone 是否开启麦克风
 */
export function rtcGroupDeviceApi(groupId, isCamera, isMicroPhone) {
  return request({
    url: "/webrtc/group/device",
    method: "post",
    data: { groupId, isCamera, isMicroPhone },
  });
}

/**
 * 群视频心跳
 * @param {number} groupId 群ID
 */
export function rtcGroupHeartbeatApi(groupId) {
  return request({
    url: "/webrtc/group/heartbeat",
    method: "post",
    params: { groupId },
  });
}


